package com.sh.data.engine.application.model.dto.integration;

import com.sh.data.engine.application.model.request.integration.OfflineSyncRequest;
import com.sh.data.engine.application.model.request.integration.OfflineSyncSchedule;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.List;

@Data
@Schema
public class OfflineSyncResponse {

    /**
     * 主键
     */
    @Schema(title = "任务id", description = "空为新增 不空为保存")
    private Long id;

    /**
     * 任务名称
     */
    @Schema(description = "任务名称")
    private String taskName;

    @Schema(description = "目录id")
    private Long dirId;

    /**
     * 任务描述
     */
    @Schema(description = "任务描述")
    private String remark;

    /**
     * 来源数据源类型
     */
    @Schema(description = "来源数据源类型")
    private String sourceDsType;

    /**
     * 来源数据源id
     */
    @Schema(description = "来源数据源id")
    private Long sourceDsId;

    @Schema(description = "来源数据源名称")
    private String sourceDsName;

    @Schema(description = "来源表 schema")
    private String sourceSchema;

    @Schema(description = "目标表 schema")
    private String targetSchema;

    /**
     * 目标数据源类型
     */
    @Schema(description = "目标数据源类型")
    private String targetDsType;

    /**
     * 目标数据源id
     */
    @Schema(description = "目标数据源id")
    private Long targetDsId;

    @Schema(description = "目标数据源名称")
    private String targetDsName;

    /** ods层级系统 */
    //  @Schema(description = "ods层级系统")
    //  private String odsName;

    /**
     * 分区定义 json
     */
    private List<PartitionField> paritions;

    @Schema(description = "1-开启配置时间，其它未开启")
    private Integer openSchedule = 1;

    /**
     * 表的增量类型 f-全量 i-增量
     */
    @Schema(description = "表的增量类型 f-全量 i-增量")
    private String tableIncType;

    @Schema(description = "来源表")
    private String sourceTable;

    @Schema(description = "目标表")
    private String targetTable;

    @Schema(description = "字段映射")
    private List<OfflineSyncRequest.FieldMapping> fieldMappings;

    /**
     * 替换规则 1-写入前清理已有数据 Insert Overwrite 2-写入前保留已有数据 Insert Into 3-同名覆盖 4-同名忽略 5-主键冲突，替换原有数据
     * 6-主键冲突，丢弃新数据 7-key值相同，覆盖 8-key值相同，忽略
     */
    @Schema(
        description =
            "替换规则 1-写入前清理已有数据 Insert Overwrite 2-写入前保留已有数据 Insert Into 3-同名覆盖 4-同名忽略 5-主键冲突，替换原有数据 6-主键冲突，丢弃新数据 7-key值相同，覆盖 8-key值相同，忽略")
    private Integer dataReplaceRuleId;

    @Schema(description = "同步前是否清理 1-是 0-否")
    private Integer isTruncate;

    /**
     * 失败记录数超过多少时，任务结束,值为-1时表示不限制
     */
    @Schema(description = "失败记录数超过多少时，任务结束,值为-1时表示不限制")
    private Integer errorNumLimit;

    /**
     * 是否是增量 0-否 1-是
     */
    @Schema(description = "是否是增量 0-否 1-是")
    private Integer isInc;

    /**
     * 过滤条件
     */
    @Schema(description = "过滤条件")
    private String incFilter;

    @Schema(description = "周期调度")
    private OfflineSyncSchedule schedule;

    /**
     * 每次读取数据条数
     */
    @Schema(description = "每次读取数据条数")
    private Integer fetchSize;

    /**
     * 每次写入数据条数
     */
    @Schema(description = "每次写入数据条数")
    private Integer batchSize;

    /**
     * 并发数
     */
    @Schema(description = "并发数")
    private Integer channel;

    /**
     * 切片字段
     */
    @Schema(description = "切片字段")
    private String splitKey;

    @Schema(description = "1-开启文件写入模式")
    private Integer pgText;

    @Data
    public static class TableMapping {

        @Schema(description = "来源表")
        private String sourceTable;

        @Schema(description = "目标表")
        private String targetTable;

        @Schema(description = "目标表表描述")
        private String targetTableComment;

        @Schema(description = "目标表是否存在")
        private Integer targetTableIsExisted = 1;

        @Schema(description = "字段映射")
        private List<FieldMapping> fieldMappings;

        @Schema(description = "分区信息")
        private List<PartitionField> paritionFields;
    }

    @Data
    @Schema
    public static class FieldMapping {

        @Schema(description = "来源字段名")
        private String sourceField;

        @Schema(description = "来源字段类型")
        private String sourceFieldType;

        @Schema(description = "来源字段是否是主键")
        private Integer isSourceFieldPk;

        @Schema(description = "目标字段名")
        private String targetField;

        @Schema(description = "目标字段类型")
        private String targetFieldType;

        @Schema(description = "目标字段是否是主键")
        private Integer isTargetFieldPk;
    }

    @Data
    @Schema
    public static class PartitionField {

        @Schema(description = "分区名名称")
        private String fieldName;

        @Schema(description = "分区值")
        private String fieldValue;
    }
}
